﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Aspose.Excel;
using Telerik.Web.UI;

public partial class StudentSectionGrades : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
        Fill();
        
      }

    }

    protected override void Fill()
    {
      try
      {
        DataLayer dal = new DataLayer();
        string message = string.Empty;

        DataTable dt = null;
        dt = dal.GetSectionGrades(PAConfiguration.ConnectionString, Teacher.TEACHER_ID, this.StudentId, this.SectionId, out message);
        if (dt == null)
        {
          rgSudentSectionGrades.DataSource = null;
          rgSudentSectionGrades.DataBind();
          return;
        }

        if (!string.IsNullOrEmpty(Sorting))
        {
          dt.DefaultView.Sort = Sorting;
        }

        rgSudentSectionGrades.DataSource = dt;
        rgSudentSectionGrades.DataBind();

        Student stud = dal.ReadStudent(PAConfiguration.ConnectionString, this.StudentId, out message);
        hlStudentName.Text = stud.LASTFIRST;
        hlStudentName.NavigateUrl = string.Format("~/ReportCard.aspx?studentId={0}", this.StudentId);
        lbStudentName.Text = string.Format("/{0}", this.CourseName);
      }
      catch (Exception e)
      {
        Response.Redirect("Error.aspx");
      }
    }

    protected void rgSudentSectionGrades_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
      if (e.Item is GridDataItem)
        if (e.Item.RowIndex == 2)
        {
          e.Item.Cells[2].Font.Bold = true;
          e.Item.Cells[6].Font.Bold = true;
        }
    }

    protected void rgSudentSectionGrades_SortCommand(object source, Telerik.Web.UI.GridSortCommandEventArgs e)
    {
      Sort(e);
    }

    protected void rgSudentSectionGrades_PageIndexChanged(object source, Telerik.Web.UI.GridPageChangedEventArgs e)
    {
      Fill();
    }

    protected void lbExport_Click(object sender, ImageClickEventArgs e)
    {
      License license = new License();
      string path = Server.MapPath("Aspose.Excel.lic");
      license.SetLicense(path);

      Excel ex = new Excel();
      Worksheet ws = ex.Worksheets[0];

      DataLayer dal = new DataLayer();
      string message = string.Empty;

      DataTable dt = null;
      dt = dal.GetSectionGrades(PAConfiguration.ConnectionString, Teacher.TEACHER_ID, this.StudentId, this.SectionId, out message);

      int startRow = 0;
      Utils.GridToExcel(dt, rgSudentSectionGrades, ws, true, ref startRow);

      ex.Save("StudentSectionGrades.xls", FileFormatType.Excel2003, SaveType.OpenInExcel, HttpContext.Current.Response);
      HttpContext.Current.Response.End();
    }

    protected string CourseName
    {
      get
      {
        if (string.IsNullOrEmpty(Request.QueryString["CLASS"]))
          return "";

        return Request.QueryString["CLASS"].ToString();
      }
    }
}